package educationaladministrationsystem.CSM.Mapper.Service.StudentAndTeacher.Teacher;

import educationaladministrationsystem.CSM.Mapper.Service.ServiceInterface.StudentAndTeacherInterface.Teacher.TeacherCourseManagement;
import educationaladministrationsystem.Class.Course;
import educationaladministrationsystem.CSM.Mapper.StudentAndTeacher.Teacher.TeacherCourseManagementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherCourseManagementService implements TeacherCourseManagement {
    @Autowired
    private TeacherCourseManagementMapper teacherCourseManagementMapper;

    @Override
    public List<Course> getAllCourse(String id, int page, int pageSum) {
        return teacherCourseManagementMapper.getAllCourse(id, (page - 1) * pageSum, pageSum);
    }

    @Override
    public String getTeacherNameById(String id) {
        return teacherCourseManagementMapper.getTeacherNameById(id);
    }


    @Override
    public boolean isTheSameTimeCourse(String UID, int day, int startTime, int constTime, String classId) {
        classId = classId == null ? "000" : classId;        // 可能出现classId为null的情况
        return teacherCourseManagementMapper.isTheSameTimeCourse(UID, day, startTime, startTime + constTime - 1, classId) == 0;
    }

    @Override
    public String getCourseId() {
        String result = String.valueOf(teacherCourseManagementMapper.getCourseId() + 1);
        return result.length() == 1 ? "00" + result : "0" + result;
    }

    @Override
    public void addNewCourse(Course course) {
        System.out.println(course);
        teacherCourseManagementMapper.addNewCourse(course);
    }

    @Override
    public Course getCourseById(String id) {
        return teacherCourseManagementMapper.getCourseById(id);
    }

    @Override
    public void updateCourse(Course course, String oldId) {
        teacherCourseManagementMapper.updateCourse(course, oldId);
    }

    @Override
    public void deleteCourse(String id) {
        teacherCourseManagementMapper.deleteCourse(id);
    }

    @Override
    public int getCoursePage(String id, int pageSum) {
        double sum = teacherCourseManagementMapper.getCourseCount(id);
        return (int) Math.ceil(sum / pageSum);
    }
}
